// pages/search_page/search_page.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    is_fuc:false,
    cart_num:0,
    allprice:0,
    low_img: "/statics/img/must_low_price.png",
    is_search:false,
    search_value:'',
    shop_list:[],
    balls: [
      { isShow: false, animationData: null },
      { isShow: false, animationData: null }
    ]
  },
  add_cart: function (e) {
    app.ball_animate(e, this)
    var that = this
    var shop_list = this.data.shop_list
    var specval_id = e.currentTarget.dataset.specval_id
    for (let i in shop_list) {
      if (shop_list[i].specval_id == specval_id) {
        shop_list[i].loading = true
      }
      for (let j in shop_list[i].list) {
        if (shop_list[i].list[j].specval_id == specval_id) {
          shop_list[i].list[j].loading = true
          break
        }
      }
      
    }
    this.setData({
      shop_list: shop_list
    })
    app.add_cart(e, this, function (id) {
      app.all_price(that)
      var shop_list = that.data.shop_list
      for (let i in shop_list) {
        if (shop_list[i].specval_id == specval_id) {
          shop_list[i].loading = false
        }
        for (let j in shop_list[i].list) {
          if (shop_list[i].list[j].specval_id == id) {
            shop_list[i].list[j].loading = false
            break
          }
        }
        
      }
      that.setData({
        shop_list: shop_list
      })
    });
  },
  add_freq: function (e) {
    app.ajax({
      url: app.host + 'mall/Freqlist/add_freq',
      data: {
        goods_id: e.currentTarget.dataset.goods_id
      },
      success: function (res) {
        console.log(res);
        wx.showToast({
          title: res.data.msg,
        })
      },
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var value = options.value
    var that = this
    this.setData({
      search_value: value
    })
    app.ajax({
        url: app.host + 'mall/goods/search',
      data: {
        name: value
      },
      success: function (res) {
        var shop_list = res.data.data.list
        for (let i in shop_list) {
          shop_list[i].index = i
          shop_list[i].show_spece = true
          shop_list[i].choose_text = "选规格"
        }
        that.setData({
          shop_list: shop_list
        })
      }
    })
  },
  get_val: function (e) {
      this.setData({
        search_value: e.detail.value
      })
  },
  back:function(){
    wx.navigateBack({
      delta: 1,
    })
  },
  search:function(){
    var that = this
    app.ajax({
      url: app.host + "mall/goods/search?name=" + this.data.search_value,
      success:function(res){
        var shop_list = res.data.data.list
        for (let i in shop_list) {
          shop_list[i].index = i
          shop_list[i].show_spece = true
          shop_list[i].choose_text = "选规格"
        }
        that.setData({
          shop_list: shop_list
        })
        app.set_stor(that.data.search_value)
      }
    })
  },
  confirm_search: function (e) {
    wx.redirectTo({
      url: '/pages/search_page/search_page?value=' + e.detail.value,
    })
    app.set_stor(e.detail.value)
  },
  choose_G: function (e) {
    var index = e.currentTarget.dataset.index
    var shop_list = this.data.shop_list
    if (shop_list[index].show_spece) {
      shop_list[index].show_spece = false
      shop_list[index].choose_text = "收起"
    } else {
      shop_list[index].show_spece = true
      shop_list[index].choose_text = "选规格"
    }
    this.setData({
      shop_list: shop_list
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    app.cart_num(this)
    app.all_price(this)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onLoad()
    this.onShow()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})